﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Macad.Common;
using Macad.Common.Evaluator;
using Macad.Core;
using Macad.Core.Shapes;
using Macad.Core.Topology;
using Macad.Interaction;
using Macad.Occt;
using SpCad.Window;

namespace SpCad.Utils
{
    public class Select: ISpTools
    {
        public event SpCad.Utils.SelectAction.EventHandler SelectEvent;
        public Color SelectColor { get; set; } = AIS_Shape_Color.FilteredSubshapes;
        public InteractiveContext SpContext => InteractiveContext.Current;
        public CadControl CadControl;
        private    AIS_Shape  _AisShape ;
        public SelectAction SelectAction { get; set; } 
        private List<TopoDS_Shape> SelectShape { get; } = new List<TopoDS_Shape>();
        public void SelectType(SubshapeTypes Type,InteractiveEntity sourceEntity = null, ISelectionFilter selectionFilter = null,Color ? _Color = null )
        {
            SelectAction = new SelectAction(Type, sourceEntity,selectionFilter);

            SelectAction.WorkspaceController = SpContext.WorkspaceController;
            SelectAction.Start();
            
            SelectColor = _Color ?? SelectColor;

            SelectAction.Finished += (e, a) => SelectEvent?.Invoke(e,a);
        }
     
        /// <summary>
        /// 添加选择对象
        /// </summary>
        /// <param name="edg"></param>
        public void AddSelectShape(TopoDS_Shape edg,Color? _Color=null)
        {
            SelectColor = _Color ?? SelectColor;
            SelectShape.Add(edg);

            SetAis_Shape_Face(edg);
            InteractiveContext.Current.WorkspaceController.Workspace.AisContext.Display(_AisShape, false);
            InteractiveContext.Current.WorkspaceController.Workspace.AisContext.Deactivate(_AisShape);
        }
        private AIS_Shape SetAis_Shape_Face(TopoDS_Shape edg)
        {
            _AisShape = new AIS_Shape(edg);
            _AisShape.SetZLayer(-2); // Graphic3d_ZLayerId_Top
            _AisShape.SetColor(SelectColor.ToQuantityColor());
            _AisShape.Attributes().LineAspect().SetColor(new Color(0.0f, 0.0f, 1.0f).ToQuantityColor());
            _AisShape.SetWidth(3);
            _AisShape.Attributes().PointAspect().SetScale(3);
            return _AisShape;
        }
        /// <summary>
        /// 清除选择对象
        /// </summary>
        public void ClearShape()
        {
            if (_AisShape != null)
            {
                SelectShape.Clear();
                // Remove AIS Shape
                InteractiveContext.Current.WorkspaceController.Workspace.AisContext.Remove(_AisShape, false);
                InteractiveContext.Current.WorkspaceController.Workspace.AisContext.Erase(_AisShape, false);
                _AisShape = null;
            }
        }
        /// <summary>
        /// TopoDS_Shape转为SubshapeReference
        /// </summary>
        /// <param name="_TargetBody"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public SubshapeReference GetSubshapeReference(Body _TargetBody, TopoDS_Shape args) => _TargetBody.Shape.GetSubshapeReference(_TargetBody.Shape.GetTransformedBRep(), args);


    }
}
